package com.huawei.hwCloudJs.service.locationapi;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.location.LocationManager;
import android.os.Build.VERSION;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebView;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.baidu.location.Poi;
import com.huawei.hwCloudJs.annotation.JSClassAttributes;
import com.huawei.hwCloudJs.annotation.JSMethod;
import com.huawei.hwCloudJs.api.ILocDialog;
import com.huawei.hwCloudJs.core.JSRequest;
import com.huawei.hwCloudJs.core.JsCallback;
import com.huawei.hwCloudJs.core.extKit.b;
import com.huawei.hwCloudJs.service.locationapi.a.a;
import com.huawei.hwCloudJs.service.locationapi.bean.Address;
import com.huawei.hwCloudJs.service.locationapi.bean.Coordinates;
import com.huawei.hwCloudJs.service.locationapi.bean.LocOption;
import java.lang.ref.WeakReference;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@JSClassAttributes
public class GetLocationApi extends JSRequest {
    private static final String a = "GetLocationApi";
    private double b = 0.0d;
    private double c = 0.0d;
    private LocationClient d;
    private b e;
    private Dialog f;

    private void a() {
        if (this.d != null) {
            Log.i(a, "stopGps");
            this.d.unRegisterLocationListener(this.e);
            this.d.stop();
        }
    }

    private void a(Context context, LocOption locOption, JsCallback jsCallback) {
        Dialog b = b(context, locOption, jsCallback);
        Activity activity = (Activity) new WeakReference((Activity) context).get();
        if (b != null && !b.isShowing() && activity != null && !activity.isFinishing()) {
            try {
                b.show();
            } catch (Exception e) {
                Log.e(a, "openGpsDialog is show failed");
            }
        }
    }

    private void a(Address address, BDLocation bDLocation) {
        address.setCountry(bDLocation.getCountry());
        address.setCity(bDLocation.getCity());
        address.setCityCode(bDLocation.getCityCode());
        address.setDistrict(bDLocation.getDistrict());
        if (bDLocation.getPoiList() != null && bDLocation.getPoiList().size() > 0) {
            address.setPoiName(((Poi) bDLocation.getPoiList().get(0)).getName());
        }
        address.setProvince(bDLocation.getProvince());
        address.setStreet(bDLocation.getStreet());
        address.setStreetNum(bDLocation.getStreetNumber());
    }

    private void a(Coordinates coordinates, BDLocation bDLocation) {
        coordinates.setLongitude(this.b);
        coordinates.setLatitude(this.c);
        coordinates.setAccuracy(bDLocation.getRadius());
        coordinates.setAltitude(bDLocation.getAltitude());
        coordinates.setHeading(bDLocation.getDirection());
        coordinates.setSpeed((bDLocation.getSpeed() * 1000.0f) / 3600.0f);
    }

    private void a(LocOption locOption) {
        if (this.d != null && locOption != null) {
            LocationClientOption locationClientOption = new LocationClientOption();
            if (locOption.getEnableHighAccuracy()) {
                locationClientOption.setLocationMode(LocationMode.Hight_Accuracy);
            } else {
                locationClientOption.setLocationMode(LocationMode.Battery_Saving);
            }
            locationClientOption.setCoorType(locOption.getCoordsType());
            Log.i(a, "getGeocode" + locOption.getGeocode());
            locationClientOption.setIsNeedAddress(locOption.getGeocode());
            locationClientOption.setTimeOut(locOption.getTimeout());
            this.d.setLocOption(locationClientOption);
        }
    }

    private void a(String str, boolean z) {
        if (!TextUtils.isEmpty(str)) {
            Log.i(a, "setappid" + str);
            a.a().a(str, z);
        }
    }

    private boolean a(Context context) {
        return VERSION.SDK_INT <= 22 || b(context);
    }

    private boolean a(int[] iArr) {
        return iArr != null && iArr.length >= 1 && iArr[0] == 0;
    }

    private Dialog b(Context context, LocOption locOption, JsCallback jsCallback) {
        ILocDialog iLocDialog = (ILocDialog) com.huawei.hwCloudJs.core.extKit.a.b(b.a, ILocDialog.class);
        if (iLocDialog != null) {
            this.f = iLocDialog.genDialog(context, locOption.getWebUrl(), new 2(this, locOption, context, jsCallback));
        }
        return this.f;
    }

    private boolean b(Context context) {
        LocationManager locationManager = (LocationManager) context.getSystemService("location");
        if (locationManager == null) {
            return false;
        }
        return locationManager.isProviderEnabled("gps") || locationManager.isProviderEnabled("network");
    }

    private boolean b(String str) {
        return !TextUtils.isEmpty(str) ? a.a().a(str) : false;
    }

    private static long c(String str) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(str).getTime();
        } catch (ParseException e) {
            Log.e(a, "date2TimeStamp Exception!");
            return 0;
        } catch (Exception e2) {
            Log.e(a, "date2TimeStamp Exception!");
            return 0;
        }
    }

    private List<String> c(Context context) {
        int i = 0;
        Object arrayList = new ArrayList();
        if (VERSION.SDK_INT > 22) {
            String[] strArr = new String[]{"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"};
            int length = strArr.length;
            while (i < length) {
                String str = strArr[i];
                if (context.checkSelfPermission(str) != 0) {
                    arrayList.add(str);
                }
                i++;
            }
        }
        return arrayList;
    }

    private void c(Context context, LocOption locOption, JsCallback jsCallback) {
        if (this.d == null) {
            this.d = new LocationClient(context);
        }
        a(locOption);
        this.d.registerLocationListener(this.e);
        this.d.start();
    }

    @TargetApi(23)
    private void d(Context context, LocOption locOption, JsCallback jsCallback) {
        boolean a = a(context);
        List c = c(context);
        if (!a) {
            Log.e(a, "sysLocateEnabled false!");
            jsCallback.failure(1, "sysLocateEnabled false!");
        } else if (c.isEmpty()) {
            c(context, locOption, jsCallback);
        } else {
            Activity activity = (Activity) new WeakReference((Activity) context).get();
            if (activity == null) {
                jsCallback.failure(1, "locationPermissionCheck false!");
                return;
            }
            a.a().a(new 3(this, context, locOption, jsCallback));
            activity.requestPermissions((String[]) c.toArray(new String[c.size()]), com.huawei.hwCloudJs.b.o);
        }
    }

    @JSMethod(isOpen = true, permission = "com.huawei.javascript.base")
    public void getCurrentPosition(LocOption locOption, JsCallback jsCallback) {
        Log.e(a, "getCurrentPosition done");
        WebView webView = jsCallback.getWebView();
        if (webView == null || locOption == null) {
            jsCallback.failure(4, "webView == null ||null == locOption!");
            return;
        }
        Context context = webView.getContext();
        this.e = new b(new 1(this, jsCallback, locOption));
        if (!com.huawei.hwCloudJs.b.a.b() || b(locOption.getAppId())) {
            d(context, locOption, jsCallback);
        } else {
            a(context, locOption, jsCallback);
        }
    }
}
